﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Recipe.Data;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace Recipe
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class RecipePage : Recipe.Common.LayoutAwarePage
    {
        public static string RecipeItemId { get; set; }

        public RecipePage()
        {
            this.InitializeComponent();
            this.BindData();   

            var html = @"<iframe name=""alimamaifrm"" frameborder=""0"" marginheight=""0"" marginwidth=""0"" border=""0"" scrolling=""no"" width=""470"" height=""400"" src='http://zlddtt.taobao.com' target='_blank' ></iframe>";
            var adetao = @"<iframe name=""alimamaifrm"" frameborder=""0"" marginheight=""0"" marginwidth=""0"" border=""0"" scrolling=""no"" width=""470"" height=""70"" src=""http://www.taobao.com/go/act/etao/etao-promotion.php?pid=mm_11866744_2169336_11516980&g_etao=1&g_lg=1&g_w=372&g_h=69&g_txt=%E8%BF%94%E5%88%A9&g_hot=1&g_hc=DF600E"" ></iframe>";
            this.webView.NavigateToString(@"<html><body>" + adetao + html + "</body></html>");           
        }
      

        private void BindData()
        {
            if (!string.IsNullOrEmpty(RecipeItemId))
            {
                var ids = RecipeItemId.Split('-');
                var groupId = ids[1];
                var itemId = ids[ids.Length - 1];

                XDocument xmlDoc = XDocument.Load("Data\\RecipeData.xml");

                var rGroup = from g in xmlDoc.Descendants("groups").Elements("group")
                             where g.Attribute("id").Value == groupId
                             select g;

                var items = from i in rGroup.Descendants("items").Elements("item")
                            where i.Attribute("id").Value == itemId
                            select new DataItem(
                                    i.Attribute("id").Value,
                                    i.Attribute("title").Value,
                                    i.Element("subtitle").Value,
                                    i.Attribute("img").Value,
                                    i.Element("description").Value,
                                    i.Element("content").Value,
                                    null,
                                    (i.Descendants("steps").Elements("step").ToList()).Select(e => e.Value).ToList(),
                                    (i.Descendants("materials").Elements("material").ToList())
                                     .Select(m => new Material(
                                                                 0,
                                                                 m.Attribute("name").Value,
                                                                 m.Attribute("unit").Value,
                                                                 m.Attribute("quantity").Value,
                                                                 m.Attribute("img").Value
                                                               )).ToList());

                DataItem r = items.FirstOrDefault();

                if (null != r)
                {
                    this.imgBig.Source = r.Image;
                    this.txtDescription.Text = r.Description;
                    this.txtTitle.Text = r.Title;

                    foreach (var s in r.Steps)
                    {
                        this.txtSteps.Inlines.Add(new Run { Text = s, FontWeight = FontWeights.Normal, Foreground = new SolidColorBrush(Colors.White), FontSize = 16 });
                        this.txtSteps.Inlines.Add(new LineBreak());
                        this.txtSteps.Inlines.Add(new LineBreak());
                    }

                    foreach (var m in r.Materials)
                    {
                        StackPanel sp = new StackPanel();

                        sp.HorizontalAlignment = HorizontalAlignment.Left;
                        sp.Height = 55;
                        sp.Margin = new Thickness(0, 10, 0, 0);
                        sp.Width = 330;
                        sp.Background = new SolidColorBrush(Colors.SeaGreen);
                        sp.Orientation = Orientation.Horizontal;

                        Image img = new Image();
                        img.Height = 55;
                        img.Margin = new Thickness(0);
                        img.Source = m.Image;
                        img.Stretch = Stretch.Fill;
                        img.Width = 55;
                        img.HorizontalAlignment = HorizontalAlignment.Left;
                        img.VerticalAlignment = VerticalAlignment.Top;

                        sp.Children.Add(img);
                        TextBlock txt = new TextBlock();
                        txt.Height = 57;
                        txt.Margin = new Thickness(10, 0, 0, 0);
                        txt.Text = m.Name + " " + m.Quantity + " " + m.Unit;
                        txt.Width = 250;
                        txt.HorizontalAlignment = HorizontalAlignment.Left;
                        txt.VerticalAlignment = VerticalAlignment.Center;
                        txt.Foreground = new SolidColorBrush(Colors.White);
                        txt.Padding = new Thickness(0, 10, 0, 0);
                        txt.FontFamily = new FontFamily("Segoe UI");
                        txt.FontSize = 28;
                        sp.Children.Add(txt);
                        this.spShiCai.Children.Add(sp);
                    }
                }

            }

        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private void btnHome_Click(object sender, RoutedEventArgs e)
        {
            this.GoHome(sender, e);
        }

        private void imgLogo_Tapped_1(object sender, TappedRoutedEventArgs e)
        {
            this.GoHome(sender, e);
        }
    }
}
